package com.yunsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.dao.YunscStoreMenuDao;
import com.yunsc.dao.YunscStoreRoleMenuDao;
import com.yunsc.dao.YunscStoreUserDao;
import com.yunsc.dao.YunscStoreUserRoleDao;
import com.yunsc.module.dto.StoreUserInfoDto;
import com.yunsc.module.entity.YunscStoreMenuEntity;
import com.yunsc.module.entity.YunscStoreRoleMenuEntity;
import com.yunsc.module.entity.YunscStoreUserEntity;
import com.yunsc.module.entity.YunscStoreUserRoleEntity;
import com.yunsc.service.YunscStoreUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("yunscStoreUserService")
public class YunscStoreUserServiceImpl extends ServiceImpl<YunscStoreUserDao, YunscStoreUserEntity> implements YunscStoreUserService {

    @Resource
    private YunscStoreUserRoleDao storeUserRoleDao;
    @Resource
    private YunscStoreRoleMenuDao storeRoleMenuDao;

    @Resource
    private YunscStoreMenuDao storeMenuDao;

    @Override
    public StoreUserInfoDto getUserInfo(Integer userId) {
        StoreUserInfoDto userInfoDto = new StoreUserInfoDto();
        YunscStoreUserEntity storeUser = this.getById(userId);
        storeUser.setPassword(null);
        userInfoDto.setUserInfo(storeUser);
        // 获取用户权限
        YunscStoreUserRoleEntity userRoleEntity = storeUserRoleDao.selectOne(new LambdaQueryWrapper<YunscStoreUserRoleEntity>()
                .eq(YunscStoreUserRoleEntity::getStoreUserId, userId));
        Integer roleId = userRoleEntity.getRoleId();
        // 获取角色对应菜单权限id
        List<YunscStoreRoleMenuEntity> storeRoleMenuList = storeRoleMenuDao.selectList(new LambdaQueryWrapper<YunscStoreRoleMenuEntity>()
                .eq(YunscStoreRoleMenuEntity::getRoleId, roleId)
                .eq(YunscStoreRoleMenuEntity::getStoreId, 10001));
        List<Integer> menuIds = storeRoleMenuList.stream().map(YunscStoreRoleMenuEntity::getMenuId).collect(Collectors.toList());
        // 获取角色对应菜单权限
        List<YunscStoreMenuEntity> menuList = storeMenuDao.selectBatchIds(menuIds);
        List<StoreUserInfoDto.Permission> permissionList = new ArrayList<>();
        menuList.forEach(menu -> {
            StoreUserInfoDto.Permission permission = new StoreUserInfoDto.Permission();
            permission.setName(menu.getName());
            permission.setPermissionId(menu.getPath());
            permission.setActionEntitySet(getActionEntitySet(menu, menuList));
            if (StringUtils.isNotEmpty(menu.getPath())) {
                permissionList.add(permission);
            }
        });
        StoreUserInfoDto.Roles roles = new StoreUserInfoDto.Roles();
        roles.setIsSuper(storeUser.getIsSuper());
        roles.setPermissions(permissionList);
        userInfoDto.setRoles(roles);

        return userInfoDto;
    }
    private List<StoreUserInfoDto.ChildPer> getActionEntitySet(YunscStoreMenuEntity menu, List<YunscStoreMenuEntity> menuList) {
        List<StoreUserInfoDto.ChildPer> actionEntitySet = new ArrayList<>();
        menuList.forEach(menuEntity -> {
            if (Objects.equals(menuEntity.getParentId(), menu.getMenuId())) {
                StoreUserInfoDto.ChildPer childPer = new StoreUserInfoDto.ChildPer();
                childPer.setAction(menuEntity.getActionMark());
                childPer.setDescribe(menuEntity.getName());
                actionEntitySet.add(childPer);
            }
        });
        return actionEntitySet;
    }

}